package com.jxs.android.weiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;

import com.telecom.weixi.R;

public class FilterView extends Button {

	private Context mContext;
	private FilterViewWinPop mPopupWindow;

	public FilterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initButton(context);
	}

	private void initButton(Context context) {
		this.mContext = context;
		// UMSpinnerButton监听事件
		setOnClickListener(new FilterViewOnClickListener());
	}

	public PopupWindow getPopupWindow() {
		return mPopupWindow;
	}

	public void setPopupWindow(FilterViewWinPop mPopupWindow) {
		this.mPopupWindow = mPopupWindow;
	}

	public void dismiss() {
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
		}
	}

	public void setViewAndCreatedListener(View contentView, FilterViewCreatedListener createdListener) {
		mPopupWindow = new FilterViewWinPop(mContext, contentView);
		mPopupWindow.setFilterViewCreatedListener(createdListener);
	}

	/**
	 * UMSpinnerButton的点击事件
	 */
	private class FilterViewOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (mPopupWindow != null) {
				if (!mPopupWindow.isShowing()) {
					// 设置PopupWindow弹出,退出样式
					mPopupWindow.setAnimationStyle(R.style.Bu_Animation_Dropdown);
					// 计算popupWindow下拉x轴的位置
					int lx = (FilterView.this.getWidth() - mPopupWindow.getmViewWidth() - 7) / 2;
					// showPopupWindow
					mPopupWindow.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.complete_translate));
					mPopupWindow.showAsDropDown(FilterView.this, lx, 5);
				}
			}
		}
	}

}
